thin.imports('/public/home/css/global.project.css');

thin.imports('/public/configure/conf.project.js');
thin.imports('/public/home/resource/pro-delete.js');

thin.use(function() {
	// 路由
	checkLocation();

	$('#btn-select-data').on('click', function(e) {
		var hash = $('input[name=iptind]:checked').attr('data-hash');
		__getdata_prodatacom['industry'] = $('input[name=iptind]:checked').attr('data-title');
		checkLocation(hash);
	});

	$('#submit-operate').on('click', function() {
		submitData('[id*=ipt-op]');
	});
	$('#submit-otherdata').on('click', function() {
		submitData('[id*=ipt-ot]');
	});

	$('#submit-topay-op').on('click', function() {
		var d = $(this).attr('data-d');
		submitData('[id*=ipt-op]', d);
	});
	$('#submit-topay-ot').on('click', function() {
		var d = $(this).attr('data-d');
		submitData('[id*=ipt-ot]', d);
	});

	// 绑定删除项目按钮
	var submitDeleteProject = thin.submitDeleteProject;
	$(document).on('click', '[layout-mark=pro-del]', submitDeleteProject);
});

var __getdata_prodatacom = {},
	__getdata_proget = {};

function submitData(itor, todo) {
	var data_opt = {};
	$(itor).each(function(index) {
		var that = $(this),
			name_arr = that.attr('id'),
			name = name_arr.split('-')[2];
		(that.val() != '') && (data_opt[name] = that.val());
	});
	$.showLoading();
	__getdata_prodatacom = tool.optionExtend(data_opt, __getdata_prodatacom);
	factory.request('_project_datacom', (todo || ''));
}

function __complete_prodatacom(res, unkn) {
	$.hideLoading();
	if(res.status == 201) {
		$('#pro_cp_uri').attr('href', res.data.cp_uri);
		if(unkn == 'topay') {
			var _proid = tool.getSearch('proid');
			// 项目评估付费
			var param = {
				mark: 'cp',
				proid: _proid,
				reurl: '/mobile/project/review?proid=' + _proid + '&cmd=recp#industry'
			}
			window.location.href = '/mobile/pay/index.html?param=' + encodeURIComponent(JSON.stringify(param));
		} else if(unkn == 'cp') {
			$.showLoading();
			__getdata_prodatacp = {
				'project_id': tool.getSearch('proid')
			}
			factory.request('_project_datacp');
		} else {
			checkLocation('project');
		}
	} else {
		$.toptip(res.error, 'error');
	}
}

function __complete_prodatacp(res) {
	$.hideLoading();
	if(res.status >= 200 && res.status < 300) {
		$.modal({
			title: "提示",
			text: "项目评估成功",
			buttons: [{
					text: "下载评估报告",
					className: 'c-ff9e80',
					onClick: function() {
						window.location.href = res.data.cp_uri;
					}
				},
				{
					text: "一键融资",
					onClick: function() {
						window.location.href = '/mobile/chain/fund';
					}
				}
			]
		});
	} else {
		$.toptip(res.error, 'error');
	}
}

function __complete_proget(res) {
	$.hideLoading();
	if(res.status == 200) {
		var isCpPay = res.data.is_cp_pay;
		if(isCpPay == 0) {
			// 未付费
			$('#submit-topay-op').addClass('weui-btn_warn').html('付费估值').attr('data-d', 'topay');
			$('#submit-topay-ot').addClass('weui-btn_warn').html('付费估值').attr('data-d', 'topay');
		} else {
			// 已付费
			$('#submit-topay-op').addClass('weui-btn_warn').html('重新估值').attr('data-d', 'cp');
			$('#submit-topay-ot').addClass('weui-btn_warn').html('重新估值').attr('data-d', 'cp');
		}
		var opt = res.data.data;
		__getdata_prodatacom['industry'] = opt.industry;
		$('input[data-title=' + opt.industry + ']').click();
		$('[id*=ipt-ot], [id*=ipt-op]').each(function(index) {
			var that = $(this),
				name_arr = that.attr('id'),
				name = name_arr.split('-')[2];
			$(this).val(opt[name]);
		});
		if (tool.getSearch('cmd') && tool.getSearch('cmd') == 'recp' && !jude_recp) {
			$('#btn-select-data').trigger('click');
		}
	} else {
		$.toptip(res.error, 'error');
	}
}

function __complete_procplist(res) {
	$.hideLoading();
	if(res.status == 200) {
		if(res.data.length > 0) {
			var ele = '';
			for(var i in res.data) {
				var data = res.data[i];
				ele += '<div class="weui-form-preview m-b-2x">';
				ele += '<a class="weui-form-preview__bd weui-cell" href="javascript:;">';
				ele += '<div class="weui-cell__hd"><img class="pro-logo" src="' + data.logo + '" alt=""></div>';
				ele += '<div class="weui-cell__bd text-left">';
				ele += '<p>' + data.title + '</p>';
				ele += '</div>';
				ele += '<div class="weui-cell__ft">' + tool.formatDate('yy-mm-dd', data.create_time) + '</div>';
				/*if(data.is_cp_pay == 0) {
					ele += '<div class="weui-cell__ft">未支付</div>';
				} else if(data.is_cp_pay == 1) {
					ele += '<div class="weui-cell__ft color-wx">已支付</div>';
				}*/
				ele += '</a>';
				ele += '<div class="weui-form-preview__ft">';
				ele += '<div class="weui-flex__item b-line-r">';
				ele += '<a class="weui-form-preview__btn c-e64340" href="javascript:;" layout-mark="pro-del" data-proid="' + data.id + '" data-iscp="' + data.is_cp_pay + '" data-iscx="' + data.is_cx_pay + '">删除项目</a>';
				ele += '</div>';
				ele += '<div class="weui-flex__item">';
				ele += '<a class="weui-form-preview__btn weui-form-preview__btn_primary" href="/mobile/project/review?proid=' + data.id + '#industry">项目评估</a>';
				ele += '</div>';
				ele += '</div>';
				ele += '</div>';
			}
			$('#project-template').html(ele);
		} else {
			checkLocation('notlists');
		}
	} else {
		$.toptip(res.error, 'error');
	}
}

var jude_plist = false,
	jude_indus = false,
	jude_recp = false;

// 路由
function checkLocationCallback(hash) {
	if(/project/gi.test(hash)) {
		if(!jude_plist) {
			$.showLoading();
			factory.request('_project_cplist');
			jude_plist = true;
		}
	}

	if(hash == 'industry') {
		var proid = tool.GetQueryString('proid');
		if(proid) {
			if(!jude_indus) {
				$.showLoading();
				__getdata_prodatacom['project_id'] = __getdata_proget['project_id'] = proid;
				__getdata_proget['model'] = 1;
				factory.request('_project_get');
			}
			jude_indus = true;
		}
	}
	
	if (hash == 'otherdata') {
		if (tool.getSearch('cmd') && tool.getSearch('cmd') == 'recp' && !jude_recp) {
			$('#submit-topay-ot').trigger('click');
			jude_recp = true;
		}
	}
	if (hash == 'operate') {
		if (tool.getSearch('cmd') && tool.getSearch('cmd') == 'recp' && !jude_recp) {
			$('#submit-topay-op').trigger('click');
			jude_recp = true;
		}
	}
}